package com.nowcoder.code.hjnc;

import com.algorithm.model.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;

public class NC15 {


    ArrayList<ArrayList<Integer>> list = new ArrayList<>();
    LinkedList<TreeNode> linkedList = new LinkedList<>();

    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return list;
        }
        linkedList.add(root);
        bfs();
        return list;
    }

    public void bfs() {
        while (!linkedList.isEmpty()) {
            int size = linkedList.size();
            ArrayList<Integer> cl = new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = linkedList.pollFirst();
                cl.add(treeNode.val);
                if (treeNode.left != null) {
                    linkedList.addLast(treeNode.left);
                }
                if (treeNode.right != null) {
                    linkedList.addLast(treeNode.right);
                }
            }
            list.add(cl);
        }
    }

}
